package com.lvy.hczn.front.web.controller.system;

import com.lvy.hczn.front.common.annotation.Log;
import com.lvy.hczn.front.common.core.controller.BaseController;
import com.lvy.hczn.front.common.core.domain.AjaxResult;
import com.lvy.hczn.front.common.core.page.TableDataInfo;
import com.lvy.hczn.front.common.enums.BusinessType;
import com.lvy.hczn.front.common.utils.poi.ExcelUtil;
import com.lvy.hczn.front.system.domain.system.SysArea;
import com.lvy.hczn.front.system.service.ISysAreaService;
import com.lvy.hczn.front.system.service.ISysAreaService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system/area/")
public class SysAreaController extends BaseController {

    private String prefix = "system/area/";

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:area:view")
    @GetMapping()
    public String dictData() {
        return prefix + "/area";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:area:list")
    @ResponseBody
    public TableDataInfo list(SysArea sysArea) {
        startPage();
        List<SysArea> list = sysAreaService.getPageList(sysArea);
        return getDataTable(list);
    }

    @Log(title = "区域管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:area:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysArea sysArea) {
        List<SysArea> list = sysAreaService.getPageList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        return util.exportExcel(list, "区域管理");
    }

    /**
     * 新增区域
     */
    @GetMapping("/add/{pid}")
    public String add(@PathVariable("pid") String dictType, ModelMap mmap) {
        mmap.put("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 新增保存区域
     */
    @Log(title = "区域", businessType = BusinessType.INSERT)
    @RequiresPermissions("system:area:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SysArea sysArea) {
        sysArea.setCreateBy(getLoginName());
        try {
            return toAjax(sysAreaService.saveData(sysArea));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改区域
     */
    @RequiresPermissions("system:area:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("area", sysAreaService.getById(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存区域
     */
    @Log(title = "区域", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:area:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SysArea sysArea) {
        sysArea.setUpdateBy(getLoginName());
        try {
            return toAjax(sysAreaService.updateById(sysArea));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @Log(title = "区域", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:area:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            sysAreaService.delData(ids);
            return success();
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequiresPermissions("system:area:list")
    @RequestMapping("/selectByDeep")
    @ResponseBody
    public List<SysArea> selectByDeep(SysArea sysArea) {
        try {
            return sysAreaService.selectByDeep(sysArea);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }
}
